package cn.com.egova.erp.filter;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * Created by yuanchao on 2020/6/15.
 */
public class ErpAuthorizationFilter extends AuthorizationFilter {
    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {
        //获取主题
        Subject subject = getSubject(servletRequest, servletResponse);
        //得到配置文件中的权限列表
        String[] perms = (String[]) o;
        //如果为空或者长度为0则放行(表示不需要授权)
        if (perms == null || perms.length == 0) {
            return true;
        }
        //权限检查(只要有一个符合就行)
        for (String p : perms) {
            if (subject.isPermitted(p)) {
                return true;
            }
        }
        return false;
    }
}
